package com.example.a97369.widgettest_306wkm;

import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView textView;//声明控件
    private Button button1,button2;
    private ImageButton imageButton;
    private EditText editText;
    private ImageView imageView;
    //声明8个控件
    private RadioGroup rg;
    private  RadioButton rbnan,rbnv;
    private CheckBox cblan,cbzu,cbyou;
    private Button btngetgender,btngetfun;
    private String strfun="";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        //获取六个控件
        textView=(TextView)findViewById(R.id.TextView1);
        button1=(Button)findViewById(R.id.button);
        button2=(Button)findViewById(R.id.button2);
        imageButton=(ImageButton)findViewById(R.id.imageButton);
        editText=(EditText)findViewById(R.id.editText5);
        imageView=(ImageView)findViewById(R.id.imageView);
        rg=(RadioGroup)findViewById(R.id.radioGroup);
        rbnan=(RadioButton)findViewById(R.id.radioButton7);
        rbnv=(RadioButton)findViewById(R.id.radioButton6);
        cblan=(CheckBox)findViewById(R.id.checkBox2);
        cbzu=(CheckBox)findViewById(R.id.checkBox3);
        cbyou=(CheckBox)findViewById(R.id.checkBox4);
        btngetgender=(Button)findViewById(R.id.button3);
        btngetfun=(Button)findViewById(R.id.button5);

        //设置按钮监听器
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        imageButton.setOnClickListener(this);
        rbnan.setOnClickListener(this);
        rbnv.setOnClickListener(this);
        cblan.setOnClickListener(this);
        cbzu.setOnClickListener(this);
        cbyou.setOnClickListener(this);
        btngetgender.setOnClickListener(this);
        btngetfun.setOnClickListener(this);

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId)
                {
                    case R.id.radioButton7:
                        textView.setText("男");
                        break;
                    case R.id.radioButton6:
                        textView.setText("女");
                        break;

                }
            }
        });
        cblan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                    strfun+="篮球";
                else
                    strfun=strfun.replace("篮球+","");
                textView.setText(strfun);
            }
        });
        cbzu.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                    strfun+="足球";
                else
                    strfun=strfun.replace("足球+","");
                textView.setText(strfun);
            }
        });
        cbyou.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                    strfun+="游泳";
                else
                    strfun=strfun.replace("游泳+","");
                textView.setText(strfun);
            }
        });





    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.button://获取并设置文本
                String str=editText.getText().toString();
                textView.setText(str);
                break;
            case R.id.button2://设置图片
                imageView.setImageResource(R.drawable.picture);
                break;
            case R.id.imageButton://图片按钮
                Intent intent=new Intent(MainActivity.this,NewActivity.class);
                intent.putExtra("param1",editText.getText().toString());
                intent.putExtra("param2",3);
                startActivity(intent);
                break;
            case R.id.button3:
                if(rbnan.isChecked())
                    textView.setText("男");
                if(rbnv.isChecked())
                    textView.setText("女");
                break;
            case R.id.button5:
                String msg="";
                if(cblan.isChecked()) msg+="篮球+";
                if(cbzu.isChecked()) msg+="足球+";
                if(cbyou.isChecked()) msg+="游泳";
                textView.setText(msg);
                break;
        }
    }
}
